{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "f21663cb-b9e7-4509-845a-1d25e9c20eef",
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain_core.prompts import ChatPromptTemplate\n",
    "from langchain_core.output_parsers import StrOutputParser\n",
    "from langchain_core.runnables import RunnablePassthrough\n",
    "\n",
    "from langchain_community.utilities import SQLDatabase\n",
    "\n",
    "from langchain.llms import Ollama\n",
    "from langchain_openai import ChatOpenAI\n",
    "\n",
    "import os"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "088fd620-d630-498d-bfc4-f0a828d1b2d9",
   "metadata": {},
   "outputs": [],
   "source": [
    "template = \"\"\"You are a SQL expert. Based on the table schema below, write just the SQL query without the results that would answer the user's question.:\n",
    "{schema}\n",
    "\n",
    "Question: {question}\n",
    "SQL Query:\"\"\"\n",
    "prompt = ChatPromptTemplate.from_template(template)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "c0037638-98bc-42b1-b378-bace7e0f68c7",
   "metadata": {},
   "outputs": [],
   "source": [
    "db = SQLDatabase.from_uri(\"sqlite:///db/northwind-SQLite3/dist/northwind.db\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "1ba19bdd-9ff4-42ba-9082-df2d63923d39",
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_schema(_):\n",
    "    return db.get_table_info()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "8f379e2d-c419-49fd-9853-d7170cc31f75",
   "metadata": {},
   "outputs": [],
   "source": [
    "def run_query(query):\n",
    "    return db.run(query)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "15ed60ad-daa0-4d40-b696-4d0af45c5db5",
   "metadata": {},
   "outputs": [],
   "source": [
    "api_key = os.environ.get('OPENAI_API_KEY')\n",
    "model = ChatOpenAI(openai_api_key=api_key)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "04474d63-0e2b-4c7f-97ab-06f09f36da2d",
   "metadata": {},
   "outputs": [],
   "source": [
    "sql_response = (\n",
    "    RunnablePassthrough.assign(schema=get_schema)\n",
    "    | prompt\n",
    "    | model.bind(stop=[\"\\nSQLResult:\"])\n",
    "    | StrOutputParser()\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "58e49460-46ce-465f-91b7-0118d340d63e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'SELECT COUNT(EmployeeID) AS TotalEmployees FROM Employees;'"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sql_response.invoke({\"question\": \"How many employees are there?\"})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "ea60d119-123f-4dd4-b32f-c6960d6cf0bc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\"SELECT COUNT(*) \\nFROM Employees \\nWHERE HireDate < DATE('now', '-11 years')\""
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sql_response.invoke({\"question\": \"How many employees have been tenured for more than 11 years?\"})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "9e9ea7a6-61c6-43f9-9ccb-494d3384411d",
   "metadata": {},
   "outputs": [],
   "source": [
    "template = \"\"\"Based on the table schema below, question, sql query, and sql response, write a natural language response:\n",
    "{schema}\n",
    "\n",
    "Question: {question}\n",
    "SQL Query: {query}\n",
    "SQL Response: {response}\"\"\"\n",
    "prompt_response = ChatPromptTemplate.from_template(template)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "e92b87e0-7cd9-465a-ab0a-a9ea57ec848e",
   "metadata": {},
   "outputs": [],
   "source": [
    "full_chain = (\n",
    "    RunnablePassthrough.assign(query=sql_response).assign(\n",
    "        schema=get_schema,\n",
    "        response=lambda x: run_query(x[\"query\"]),\n",
    "    )\n",
    "    | prompt_response\n",
    "    | model\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "5077ce07-b1e0-4be3-af88-62746147235d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "content='There are a total of 9 employees in the database.'\n"
     ]
    }
   ],
   "source": [
    "result = full_chain.invoke({\"question\": \"How many employees are there?\"})\n",
    "print(result)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "4d91f876-8ccb-4d1b-9ffa-b25c6c33b5ce",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "content='Employees who have been tenured for more than 11 years are Nancy Davolio, Andrew Fuller, Janet Leverling, and Margaret Peacock.'\n"
     ]
    }
   ],
   "source": [
    "result = full_chain.invoke({\"question\": \"Give me the name of employees who have been tenured for more than 11 years?\"})\n",
    "print(result)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "torchgpu",
   "language": "python",
   "name": "torchgpu"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
